// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchData:{},
    searchVal:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _this = this;
    //从缓存拿到历史记录
    wx.getStorage({
      key: 'search_history',
      success: function(res) {
        _this.setData({
          searchData:res.data
        });
      },
    })
  },

  bindKeyInput: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },
  toHistoryListPage:function(){
    this.toListView();
  },
  toListPage: function() {
    //把文本框的值添加到搜索记录
   // this.data.searchData.push(this.data.searchVal);
    this.data.searchData[this.data.searchVal] = this.data.searchVal;
    //放到缓存
    wx.setStorage({
      key: 'search_history',
      data: this.data.searchData,
    })
   this.toListView();
  },
  toListView:function(){
    var pages = getCurrentPages();
    var f = false;

    for (var i in pages) {
      if (pages[i].route == "pages/list/list") {
        f = true;
        break;
      }
    }

    if (!f) {
      wx.navigateTo({
        url: '/pages/list/list',
      })
    } else {
      wx.navigateBack({
        url: '/pages/list/list',
      })
    }
  }
})